<template>
  <el-form ref="form" :rules="formRules" :model="form" label-width="100px">
    <el-form-item label="设备编号:" prop="innerCode">
      <el-input v-model="form.innerCode" @blur="getUserList" />
    </el-form-item>
    <el-form-item label="工单类型:" prop="productType">
      <el-select v-model="form.productType" placeholder="请选择">
        <el-option
          v-for="item in taskList"
          :key="item.typeId"
          :label="item.typeName"
          :value="item.typeId.toString()"
        />
      </el-select>
    </el-form-item>
    <el-form-item v-if="hiden" label="补货数量:">
      <el-button class="button" icon="iconfont icon-gongdan" @click="replenishmentFn">补货清单</el-button>
    </el-form-item>
    <el-form-item label="运营人员:">
      <el-select v-model="form.assignorId" placeholder="请选择">
        <el-option
          v-for="item in userList"
          :key="item.userName"
          :label="item.userName"
          :value="item.userId"
        />
      </el-select>
    </el-form-item>
    <el-form-item label="备注" prop="desc">
      <el-input v-model="form.desc" type="textarea" placeholder="请输入备注，不超过40个字" />
    </el-form-item>
  </el-form>
</template>
<script>
import { getOperatorList, addTask, getTaskDetails, taskList } from '@/api'
export default {
  props: {
    taskId: {
      type: String,
      default: ''
    },
    detail: {
      type: Array,
      default: () => []
    },
    hiden: {
      type: Boolean,
      default: true
    }
  },
  data() {
    return {
      // 工单列表
      taskList: [],
      // 货物清单
      orderList: [],
      userList: [],
      form: {
        innerCode: '',
        productType: '',
        assignorId: '',
        desc: '',
        details: [],
        userId: this.$store.getters.id
      },
      // 验证规则
      formRules: {
        innerCode: [{ required: true, message: '此项为必填项', tigger: 'blur' }],
        productType: [{ required: true, message: '此项为必填项', tigger: 'blur' }],
        desc: [{ required: true, message: '此项为必填项', tigger: ['blur', 'change'] },
          { max: 40, message: '请输入40个字符以内', tigger: ['blur', 'change'] }]
      }
    }
  },
  async created() {
    const { data } = await taskList()
    console.log(data)
    if (this.hiden) {
      this.taskList = data.filter(item => item.typeId === 2)
    } else {
      this.taskList = data.filter(item => item.typeId !== 2)
    }
    // console.log(res)
  },
  methods: {
    // 新增公单接口调用
    // 先兜底校验
    addTaskFn() {
      this.form.details.length === 0 ? this.form.details = this.detail : ''
      this.$refs.form.validate(async val => {
        if (!val) return
        if (this.hiden) {
          if (this.form.details.length === 0) return this.$message.error('请选择补货清单!')
        }
        try {
          await addTask(
            this.form

          )
          // console.log(res)
        } catch (error) {
          this.$message.error(error.response.data)
          // console.dir(error)
        }
      })
    },
    // 获取运营人员列表
    async getUserList() {
      const { data } = await getOperatorList(this.form.innerCode)
      // console.log(data)
      this.userList = data
      // console.log(this.taskId)
      this.$emit('getTaskId', this.form.innerCode)
    },
    // 点击补货清单
    async replenishmentFn() {
      this.$refs.form.validateField('innerCode', async(callback) => {
        if (callback) return

        const { data } = await getTaskDetails(this.form.innerCode)
        // console.log('1', data)
        this.orderList = data
        this.$emit('showTwoDialog', this.orderList)
      })
    }
  }
}
</script>

<style lang="scss" scoped>
.el-select {
  width: 100%;
}
.button.el-button{
  color: #409eff;
  border: none;
}
</style>
